package com.uniapp.server.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author cyt
 * @describe
 * @create 2020-12-16 17:52
 */
public class DateUtil {
    public static Date str2date(String date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return new Date();
        }
    }

    public static Date getDateBefore(Date date, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
        return now.getTime();
    }

    public static String beforeStringDate(String date, int day, String pattern) {
        Date str2date = str2date(date, pattern);
        Date before = getDateBefore(str2date, day);
        String s = date2format(before, "yyyy-MM-dd");
        return s;
    }

    public static String date2format(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

}
